Imports System.Net.NetworkInformation
Imports Microsoft.VisualBasic.Devices

''' <summary>
''' Network methods and utilities.
''' </summary>
Public Class NetworkUtil

#Region "Private constructor"

	Private Sub New()
		' Can't create instance!
	End Sub

#End Region

#Region "Network functions"

	''' <summary>
	''' Checks if computer has internet connection available (use Google's IP).
	''' </summary>
	''' <returns>True if connection is available, otherwise false.</returns>
	Public Shared Function IsConnected() As Boolean

		Return IsConnected("google.com")

	End Function

	''' <summary>
	''' Checks if computer has internet connection available, using a specific host/IP for testing.
	''' </summary>
	''' <returns>True if connection to host/IP is available, otherwise false.</returns>
	Public Shared Function IsConnected(ByVal ip As String) As Boolean

		Try

			Using p As New Ping

				Dim reply As PingReply = p.Send(ip, 3000)

				If reply.Status = IPStatus.Success Then
					Return True
				End If

			End Using

		Catch

			Return False

		End Try

		Return False

	End Function

	''' <summary>
	''' Pings and host or IP address and returns in ms.
	''' </summary>
	''' <returns>The host name or IP address to ping.</returns>
	Public Shared Function Ping(ByVal strHost As String) As Long

		Dim p As Ping = New Ping()
		Dim r As PingReply = p.Send(strHost)

		Return r.RoundtripTime

	End Function

#End Region

End Class